package com.shchuangzhi.smartguard.core.receiver;

import android.support.v4.app.INotificationSideChannel;

import com.google.gson.Gson;
import com.google.gson.annotations.SerializedName;
import com.google.gson.reflect.TypeToken;
import com.shchuangzhi.smartguard.core.Consts;
import com.shchuangzhi.smartguard.core.Machine;
import com.shchuangzhi.smartguard.core.NotifyMessage;
import com.shchuangzhi.smartguard.core.Util;
import com.shchuangzhi.smartguard.core.command.Command;
import com.shchuangzhi.smartguard.fragment.Receiver;

import java.io.DataOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.util.List;
import java.util.Map;

/**
 * Created by j_hu on 10/31/17.
 */

public class SyncReceiverDataCommand extends Command implements Command.CommandHandler {

    public SyncReceiverDataCommand() {
        super(Consts.COMMAND_REQ_RECEIVER_DATA_SYNC);
    }

    @Override
    protected OutputStream fill(OutputStream output) {
        DataOutputStream out = new DataOutputStream(output);
        try {
            CommandRQ<SyncReceiverDataCommandRQ> rq = new CommandRQ<>();
            rq.setToken(getToken());
            rq.setData(new SyncReceiverDataCommandRQ(ReceiverInOutData.getInstance().getVer(), Machine.getInstance().getId()));
            byte[] p = new Gson().toJson(rq).getBytes();
            if (p != null && p.length > 0) {
                out.write(p);
            }
        } catch(IOException e) {
            raiseError(e);
        }
        return out;
    }

    @Override
    protected String getType() {
        return "application/json";
    }

    @Override
    protected CommandHandler getHandler(String commandName) {
        return commandName.equals(Consts.COMMAND_RESP_RECIEVER_DATA) ? this : null;
    }

    @Override
    public boolean handle(InputStream input) {
        try {
            Gson gson = new Gson();
            SyncReceiverDataCommandRS rs = (SyncReceiverDataCommandRS)gson.fromJson(
                    Util.readAll(input),
                    new TypeToken<SyncReceiverDataCommandRS>(){}.getType());
            int size = 0;
            if (rs != null) {
                if (rs.getVer() != ReceiverInOutData.getInstance().getVer()) {
                    if (rs.getList() != null && rs.getList().size() > 0) {
                        size = rs.getList().size();
                        ReceiverInOutData.getInstance().apply(
                                rs.getVer(),
                                rs.getSchoolName(),
                                rs.getSchoolNo(),
                                rs.getList(),
                                ReceiverInOutData.getInstance().new InOutStats(
                                        rs.getStats().getClassNo(),
                                        rs.getStats().getName(),
                                        rs.getStats().getValue()));
//                        this.notify(NotifyMessage.wrap(null, Consts.NOTIFY_RECEIVER_STATS_CHANGED));
//                        this.notify(NotifyMessage.wrap(null, Consts.NOTIFY_RECEIVER_DATA_VIEW_CHANGEED));
                    }
                }
            }
            this.notify(NotifyMessage.info(
                    String.format("%s<%s>",  Consts.INFO_SYNC_RECEIVER_DATA, size),
                        Consts.NOTIFY_RECEIVER_DATA_SYNC, Consts.NOTIFY_DATA_SYC_VIEW_LIST));
        }catch (Exception e) {
            this.raiseError(e);
        }
        return true;
    }

    public class SyncReceiverDataCommandRQ {
        private String ver;
        @SerializedName("class_no")
        private int no;
        public SyncReceiverDataCommandRQ(String ver, int no) {
            this.ver = ver;
            this.no = no;
        }

        public String getVer() {
            return this.ver;
        }
        public int getNo() {
            return this.no;
        }
    }

    public class SyncReceiverDataCommandRS {
        private String ver;
        @SerializedName("school_name")
        private String schoolName;
        @SerializedName("school_no")
        private String schoolNo;
        private List<SyncData> list;
        private SyncStats stats;

        public String getVer() {
            return ver;
        }
        public String getSchoolName() {
            return schoolName;
        }
        public String getSchoolNo() {
            return schoolNo;
        }
        public List<SyncData> getList() {
            return list;
        }
        public SyncStats getStats() {
            return stats;
        }
    }

    public class SyncData {
        private int no;
        private String name;
        @SerializedName("photo_url")
        private String photoURL;
        private int state;
        @SerializedName("is_male")
        private boolean isMale;

        public int getNO() {
            return no;
        }

        public String getName() {
            return name;
        }

        public String getPhotoURL() {
            return photoURL;
        }

        public int getState() {
            return state;
        }

        public boolean isMale() {
            return isMale;
        }
    }

    public class SyncStats {
        @SerializedName("class_no")
        private int classNo;
        private String name;
        private Map<Integer, Integer> value;

        public int getClassNo() {
            return classNo;
        }
        public String getName() {
            return name;
        }
        public Map<Integer, Integer> getValue() {
            return value;
        }
    }
}
